<?php

/**
 * model_menu
 */

class Model_menu extends YOU {

	public  $stt  = "";
	public  $arr  = "";
	public  $fid  = "";
	public  $act  = "";
	public  $farr = "";
	// ico
	public  $ico = array(
	"9"   => "fa-dashboard",
	"29"  => "fa-user",
	"72"  => "fa-th",
	"112" => "fa-graduation-cap",
	"160" => "fa-briefcase",
	"152" => "fa-child",
	"226" => "fa-eraser",
	"232" => "fa-wrench",
	"196" => "fa-bars",
	);

	public function __construct()
	{

		parent::__construct();
	}

	/** 读出所有的menu **/
	public function getAll()
	{
		$lists = $this->db->fetchAll("SELECT * FROM `tbl_menu` ");

		return $lists;

	}

	/** 根据fun得到info **/
	public function getInfoByFun($fun = "", $pam = "")
	{

		$row = $this->db->fetchRow("SELECT * FROM `tbl_menu` WHERE menu_fun='{$fun}' AND menu_pam=''");
		if (!empty($row)){
			return $row;
		}else{
			// get参数列出
			$gam = array();
			foreach($_GET as $key => $value){
				if($key != "a"){
					$gam["{$key}={$value}"] = 1;
				}
			}

			// 尝试
			$fun_lists = $this->db->fetchAll("SELECT * FROM `tbl_menu` WHERE menu_fun='{$fun}' AND menu_pam!='' ");
			foreach ($fun_lists as $key => $value){

				$pam = array();
				if (strpos($value['menu_pam'], "&") !== false){
					$tmp = explode("&", $value['menu_pam']);
					foreach ($tmp as $k => $v){
						if (!empty($v)){
							$pam[$v] = 1;
						}
					}
				}else{
					$pam[$value['menu_pam']] = 1;
				}

				// 匹配
				foreach ($gam as $k => $v){
					if ($pam[$k] == 1){
						$data[$value['menu_id']]++;
					}
				}
			}

			if (!empty($data)){
				$max = 0;
				foreach ($data as $key => $value){
					if ($value >= $max){
						$max = $value;
						$data_id = $key;
					}
					
				}


				$row = $this->db->fetchRow("SELECT * FROM `tbl_menu` WHERE menu_id='{$data_id}'");
				return $row;
			}


			return false;
		}
	}

	/** 根据fun得到menu_id **/
	public function getId($fun = "")
	{
		$row = $this->db->fetchRow("SELECT menu_id FROM `tbl_menu` WHERE menu_fun='{$fun}'");
		if (!empty($row)){
			return $row['menu_id'];
		}else{
			return false;
		}
	}

	// 得到当前fun的menu_id数组关系
	public function getFid($menu_id = 0)
	{
		$row = $this->db->fetchRow("SELECT menu_fid FROM `tbl_menu` WHERE menu_id='{$menu_id}'");
		if (!empty($row['menu_fid'])){
			$this->farr[] = $row['menu_fid'];
			$this->getFid($row['menu_fid']);
		}

		return $this->farr;
	}

	public function getFlist($show = "show_left", $user_pms = "")
	{
		$user_pms = unserialize($user_pms);

		$where = " WHERE 1=1 ";

		if (!empty($user_pms)){
			$user_pms[] = "10000";

			foreach($user_pms as $key => $value){
				$pms[]  = "'{$value}'";
			}

			$ids = implode(",", $pms);
			$where .= " AND menu_id IN ($ids) ";
		}

		if ($show != "all"){
			$where .= " AND {$show}='1' ";
		}

		$arr = array();

		$sql = "SELECT * FROM `tbl_menu` {$where} ORDER BY menu_order";

		$menu_lists = $this->db->fetchAll("SELECT * FROM `tbl_menu` {$where} ORDER BY menu_order");
		foreach ($menu_lists as $key => $value ){
			$arr[$value['menu_fid']][] = $value;
		}

		return $arr;
	}

	/** 构建树状数据列表 **/
	public function tree_list($id = 0, $te = "")
	{

		$data_lists = $this->arr[$id];

		// 如果有子类
		if (!empty($data_lists)){

			foreach ($data_lists as $key => $value){

				if ($key == 0 && $value['menu_fid'] != 0){
					$te = "&nbsp;&nbsp".$te."--";
				}
				$this->stt .= "<tr>";

				// 构建字符串
				$this->stt .= "<td><input name=\"menu_order_{$value['menu_id']}\" type=\"text\" value=\"{$value['menu_order']}\" size=\"2\"></td>";
				$this->stt .= "<td>{$value['menu_id']}</td>";
				if ($value['menu_fid'] != 0){
					$this->stt .= "<td>{$te}{$value['menu_name']}</td>";
				}else{
					$this->stt .= "<td>{$value['menu_name']}</td>";
				}
				$this->stt .= "<td><a href='index.php?a={$value['menu_fun']}' target='_blank'>{$value['menu_fun']}</a></td>";
				$this->stt .= "<td>{$value['menu_pam']}</td>";
				$this->stt .= "<td>{$value['create_time']}</td>";
				$this->stt .= "<td>
				<a href=\"index.php?a=menu.add&menu_fid={$value['menu_id']}\" class=\"btn btn-primary btn-xs\">添加子菜单</a>
				<a href=\"index.php?a=menu.update&menu_id={$value['menu_id']}\" class=\"btn btn-warning btn-xs\">修改</a>
				<a href=\"index.php?a=menu.del&menu_id={$value['menu_id']}\" class=\"btn btn-danger btn-xs\" onclick=\"if(confirm('确定要删除?')==false)return false;\">删除</a>								
				</td>";
				$this->stt .= "</tr>\n";
				// 调用()，将记录集中的id参数传入函数中，继续查询下级
				$this->tree_list($value['menu_id'], $te);

			}
		}

		return $this->stt;
	}

	/** 构建树状下拉选项 **/
	public function tree_select($id = 0,  $te= "")
	{

		$data_lists = $this->arr[$id];

		// 如果有子类
		if (!empty($data_lists)){

			foreach ($data_lists as $key => $value){

				if ($key == 0 && $value['menu_fid'] != 0){
					$te = $te."--";
				}
				if ($value['menu_id'] == $this->fid){
					$se = " selected ";
				}else{
					$se = "";
				}

				// 构建字符串
				if ($value['menu_fid'] != 0){
					$this->stt .= "<option value=\"{$value['menu_id']}\" {$se} >{$te}{$value['menu_name']}</option>\n";
				}else{
					$this->stt .= "<option value=\"{$value['menu_id']}\" {$se} >{$value['menu_name']}</option>\n";
				}

				// 调用()，将记录集中的id参数传入函数中，继续查询下级
				$this->tree_select($value['menu_id'], $te);

			}
		}


		return $this->stt;


	}

	/** 构建树状左侧菜单 **/
	public function tree_left($id = 0,  $te= "")
	{

		// 左侧ico设定



		$data_lists = $this->arr[$id];
		// 如果有子类
		if (!empty($data_lists)){

			foreach ($data_lists as $key => $value){

				if(empty($value['menu_fun'])){
					$to = $value['menu_url'];
				}else{
					$to = "index.php?a=".$value['menu_fun'];
					if(!empty($value['menu_pam'])){
						$to .= "&".$value['menu_pam'];
					}
				}

				if($this->farr && in_array($value['menu_id'], $this->farr) ){
					$at  = " active ";
					$atc = " class='active' ";
				}else{
					$at = "";
					$atc = "";
				}

				if (isset($this->arr[$value['menu_id']]) && is_array($this->arr[$value['menu_id']])){

					if ($value['menu_fid'] != 0){
						$this->stt .= "
						<li {$atc}>
            			<a href=\" \"><i class=\"fa fa-cube\"></i>{$value['menu_name']}<i class=\"fa fa-angle-left pull-right\"></i></a>
                    	<ul class=\"treeview-menu\">
						";
					}else{

						if($this->ico[$value['menu_id']]){
							$ico = $this->ico[$value['menu_id']];
						}else{
							$ico = "fa-user";
						}

						$this->stt .= "
						<li class=\"treeview {$at}\" >
		  				<a href=\"#\">
           			    <i class=\"fa {$ico}\"></i> <span>{$value['menu_name']}</span>
            		    <i class=\"fa fa-angle-left pull-right\"></i>
         			    </a>
          				<ul class=\"treeview-menu\">
          				";
					}
					// 调用()，将记录集中的id参数传入函数中，继续查询下级
					$this->tree_left($value['menu_id'], $te);
					$this->stt .= "</ul></li>\n";

				}else{
					$this->stt .= "<li {$atc}><a href=\"{$to}\"><i class=\"fa fa-cube\"></i>{$te}{$value['menu_name']}</a></li>";
				}
			}


		}

		return $this->stt;
	}


	/** 得到面包屑 **/
	public function getNav()
	{
		$max = 4;

		$nav = array();

		$now = $_GET['a'];

		//  当前名称
		if ($now == "default.index"){
			return $nav;
		}else{
			$data = $this->getInfoByFun($now);
			$nav[$max]['fun']  = $data['menu_fun'];
			$nav[$max]['name'] = $data['menu_name'];
			if (!empty($data['menu_fid'])){

				$max = $max - 1;

				for ($i = $max; $i >=1; $i--){
					if (!empty($data['menu_fid'])){
						$data = $this->db->fetchRow("SELECT menu_fun,menu_name,menu_fid,menu_id FROM `tbl_menu` WHERE menu_id='{$data['menu_fid']}'");
						$nav[$i]['fun']  = $data['menu_fun'];
						$nav[$i]['name'] = $data['menu_name'];
					}
				}
			}

		}
		ksort($nav);
		return $nav;

	}

}



?>